package com.waimai.controllor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.waimai.constant.FileConstant;
import com.waimai.log.Log;
import com.waimai.utils.DateUtils;

public class FileControllor extends BaseControllor {
	private static boolean hasDelete = false;
	private static FileControllor controllor;

	// private static String infoDir = null;
	// private static File infoFile = null;

	private static String logParentDir = null;

	public static FileControllor getInstance() {
		if (controllor == null) {
			controllor = new FileControllor();
		}
		return controllor;
	}

	// private String getInfoDir() {
	// if (infoDir == null) {
	// // deng
	// File infoDirFile = new File(new File(
	// FileConstant.PROJECT_LOCATION_PATH).getParentFile()
	// .toString()
	// + "\\" + FileConstant.INFO_DIR_NAME);
	// if (!infoDirFile.exists()) {
	// infoDirFile.mkdir();
	// }
	// infoDir = infoDirFile.toString();
	// }
	// return infoDir;
	// }

	// private File getInfoFile() {
	// if (infoFile == null) {
	// infoFile = new File(getInfoDir() + "\\"
	// + FileConstant.INFO_FILE_NAME + ".txt");
	// if (!infoFile.exists()) {
	// try {
	// infoFile.createNewFile();
	// } catch (IOException e) {
	// Log.e(e, "infoFile is exists , create error");
	// e.printStackTrace();
	// }
	// }
	// }
	// return infoFile;
	// }

	// public void changeInfo(int tableStatus) {
	// String s = null;
	// switch (tableStatus) {
	// case DaoConstant.TABLE_STATUS_1:
	// s = "1";
	// break;
	// case DaoConstant.TABLE_STATUS_2:
	// s = "2";
	// break;
	// }
	// try {
	// FileOutputStream fos = new FileOutputStream(getInfoFile());
	// byte[] bytes = s.getBytes();
	// fos.write(bytes, 0, bytes.length);
	// } catch (FileNotFoundException e) {
	// Log.e(e, "fos error");
	// e.printStackTrace();
	// } catch (IOException e) {
	// Log.e(e, "file write error");
	// e.printStackTrace();
	// }
	//
	// }

	private String getLogParentDir() {
		if (logParentDir == null) {
			File infoDirFile = new File(new File(
					FileConstant.PROJECT_LOCATION_PATH).getParentFile()
					.toString()
					+ "\\" + FileConstant.LOG_PARENT_DIR_NAME);
			if (!infoDirFile.exists()) {
				infoDirFile.mkdir();
			}
			logParentDir = infoDirFile.toString();
		}
		return logParentDir;
	}

	private String getLogDir() {
		int month = DateUtils.getMonth();
		int date = DateUtils.getDate();
		// 创建新目录
		String logDirName = month + "-" + date;
		File logDir = new File(getLogParentDir() + "\\" + logDirName);
		if (!logDir.exists()) {
			logDir.mkdir();
		}
		if (date == 1 && !hasDelete) {
			hasDelete = true;
			// 删除一个月前的记录
			File logParentDir = new File(getLogParentDir());
			File[] childrenFiles = logParentDir.listFiles();
			for (File f : childrenFiles) {
				try {
					if (month != Integer.parseInt(f.getName().split("-")[0])) {
						deleteFile(f);
					}
				} catch (NumberFormatException e) {
					Log.e(e, "logDir isnt begin with num");
					e.printStackTrace();
				}
			}
		} else if (date != 1 && hasDelete) {
			hasDelete = false;
		}
		return logDir.toString();
	}

	private File getLogInfoFile() {
		String logFileName = "[info]" + DateUtils.getHours() + ".txt";
		File logFile = new File(getLogDir() + "\\" + logFileName);
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				Log.e(e, "logFile is exists , create error");
				e.printStackTrace();
			}
		}
		return logFile;
	}

	private File getLogErrorFile() {
		String logFileName = "[error]" + DateUtils.getHours() + ".txt";
		File logFile = new File(getLogDir() + "\\" + logFileName);
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				Log.e(e, "logFile is exists , create error");
				e.printStackTrace();
			}
		}
		return logFile;
	}

	// 递归删除
	private void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File files[] = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					this.deleteFile(files[i]);
				}
			}
			file.delete();
		}
	}

	/**
	 * 
	 * @param logString
	 * @param msgType
	 *            LOG_ERROR = 0; LOG_INFO = 1;
	 */
	public void printLog(String logString, int msgType) {
		try {
			File logFile = null;
			switch (msgType) {

			case FileConstant.LOG_ERROR:
				logFile = getLogErrorFile();
				break;
			default:
			case FileConstant.LOG_INFO:
				logFile = getLogInfoFile();
				break;
			}
			FileOutputStream fos = new FileOutputStream(logFile, true);
			byte[] bytes = logString.getBytes();
			fos.write(bytes, 0, bytes.length);
		} catch (FileNotFoundException e) {
			Log.e(e, "fos error");
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(e, "file write error");
			e.printStackTrace();
		}
	}
}
